// pages/nome/nome.js
// 选择名片到组
const utils = require('../../utils/util.js')

Page({
  data: {
    list: [],
    checkAll: false,
    blocksShow: false,
    chunkList: [], // 分组列表
    editorShow: false,
    chunkName: ''
  },
  ...utils.defaultShare,
  toggleSelect(event) {
    const _index = event.currentTarget.dataset.index;
    let list = [].concat(this.data.list);
    let checkAll = true;
    list.map((item, index) => {
      if (index == _index) {
        item.checked = !item.checked
      }
      if (!item.checked) {
        checkAll = false
      }
    })
    this.setData({ list, checkAll })
  },
  toggleSelectAll() {
    let list = [].concat(this.data.list);
    list.map((item, index) => {
      item.checked = !this.data.checkAll
    })
    this.setData({
      list,
      checkAll: !this.data.checkAll
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('options', options)
  },
  onShow() {
    this.getReciveList()
    this.getChunkList()
  },
  getReciveList() {
    // 获取已接收名片
    utils
      .ajax("/v1/bcs/queryUserCardHolders.do", {
        pageNo: this.data.pageNo || 1,
        pageSize: 10
      })
      .then(res => {
        if (res.code === "000000") {
          this.setData({
            list: res.data.datas,
            total: res.data.total
          });
        } else {
          console.log(res.message || "获取已接收名片失败");
        }
      })
      .catch(res => {
        utils.message(res.message || "网络错误");
      });
  },
  getChunkList(){
    utils
      .ajax("/v1/bcs/queryUserCardHolderGroups.do", {
        cardId: this.data.cardId
      })
      .then(res => {
        if (res.code === "000000") {
          let chunkList = [];
          try {
            chunkList = res.data.catetories[0].groups.map(item=>{
              return {
                name: item.name,
                id: item.groupId,
                selected: false
              }
            })
          } catch (e) {
            chunkList = [];
          }
          this.setData({
            chunkList,
          })
        } else {
          console.log(res.message || "获取标签失败");
        }
      })
      .catch(res => { });
  },
  saveToBlock() {
    // 保存到指定分组
    this.setData({
      blocksShow: true,
    })
  },
  hideBlocks(){
    this.setData({
      blocksShow: false,
    })
  },
  selectBlock(event){
    const index = event.currentTarget.dataset.index;
    const list = [...this.data.chunkList];
    list[index].selected = !list[index].selected;
    this.setData({
      chunkList: list
    })
  },
  showBlockEditor(){
    this.setData({
      editorShow: true
    })
  },
  editorCancel(){
    this.setData({
      editorShow: false
    })
  },
  editorConfirm(){
    // 发送请求，并把结果添加到chunkList中
    utils
      .ajax("/v1/bcs/createUserCardHolderGroup.do", {
        name: this.data.chunkName
      })
      .then(res => {
        if (res.code === "000000") {
          const list = [...this.data.chunkList, { name: this.data.chunkName, selected: true, id: res.data.groupId }]
          this.setData({
            editorShow: false,
            chunkName: '',
            chunkList: list
          })
        } else {
          console.log(res.message || "获取标签失败");
        }
      })
      .catch(res => { });
  },
  chunkNameChangeEvent(event){
    const value = event.detail.value;
    this.setData({
      chunkName: value
    })
  },
  saveBlockInfo(){
    // 保存分组信息
    let groupIds = this.data.chunkList.filter(item => {
      return item.selected
    }).map(item => {
      return item.id
    });
    let holderIds = this.data.list.filter(item => {
      return item.checked
    }).map(item => {
      return item.holdId
    });
    utils
      .ajax("/v1/bcs/add2UserCardHolderGroup.do", {
        groupIds,
        holderIds
      })
      .then(res => {
        if (res.code === "000000") {
          utils.message("添加分组成功")
          this.setData({
            blocksShow: false
          },()=>{
            wx.navigateBack({
              delta: 1
            })
          })
          
        } else {
          console.log(res.message || "获取标签失败");
        }
      })
      .catch(res => { });

  }

})